<template>
  <div class="columns py-6">
    <div class="column is-half is-offset-one-quarter">
      <el-card shadow="never">
        <div slot="header" class="has-text-centered has-text-weight-bold">
          {{ $t('Login.signIn') }}
        </div>
        <div>
          <el-form
            ref="ruleForm"
            v-loading="loading"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item :label="$t('Login.account')" prop="name">
              <el-input v-model="ruleForm.name" />
            </el-form-item>

            <el-form-item :label="$t('Login.password')" prop="password" @keyup.enter.native="submitForm('ruleForm')">
              <el-input
                v-model="ruleForm.password"
                type="password"
                autocomplete="off"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">
                {{ $t('Login.signIn') }}
              </el-button>
              <el-button type="primary" @click="reSetForm('ruleForm')">
                {{ $t('Login.reset') }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script  type="text/javascript">
export default {
  name: "Login",
  data() {
    return {
      redirect: undefined,
      loading: false,
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [
          { required: true, message: this.$t('Login.blankAccount'), trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: this.$t('Login.errAccount'),
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: this.$t('Login.blankPassword'), trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: this.$t('Login.errPassword'),
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.ruleForm)
            .then(() => {
              this.$message({
                message: this.$t('Login.successIn'),
                type: "success",
                duration: 2000,
              });
              this.$store.dispatch("user/getInfo");
              setTimeout(() => {
                this.loading = false;
                this.$router.push({ path: this.redirect || "/" });
              }, 0.1 * 1000);
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    reSetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>